package com.light.util;

import atlantafx.base.theme.Styles;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.kordamp.ikonli.bootstrapicons.BootstrapIcons;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.Objects;
import java.util.concurrent.Callable;

public class FxUtil {

    /**
     * 获取资源文件
     *
     * @param resources
     * @return
     */
    public static String getResource(String resources) {
        return Objects.requireNonNull(FxUtil.class.getResource(resources)).toExternalForm();
    }

    /**
     * 获取图片
     *
     * @param resources
     * @return
     */
    public static Image getImage(String resources) {
        return new Image(getResource(resources));
    }

    /**
     * 获取图片组件
     *
     * @param resources
     * @param size
     * @return
     */
    public static ImageView getImageView(String resources, double size) {
        return getImageView(resources, size, size);
    }

    /**
     * 获取图片组件
     *
     * @param resources
     * @param height
     * @param width
     * @return
     */
    public static ImageView getImageView(String resources, double height, double width) {
        ImageView imageView = new ImageView(getResource(resources));
        imageView.setFitHeight(height);
        imageView.setFitWidth(width);
        return imageView;
    }

    /**
     * 获取组件在屏幕中的位置
     *
     * @param node
     * @return
     */
    public static Bounds localToScreen(Node node) {
        return node.localToScreen(node.getLayoutBounds());
    }

    /**
     * 获取窗口
     *
     * @param node
     * @return
     */
    public static Stage getStage(Node node) {
        return (Stage) getWindow(node);
    }


    /**
     * 获取当前被聚焦的第一个窗口，没有则返回null。
     *
     * @return
     */
    public static Window getFocusedWindow() {
        return Stage.getWindows().stream().filter(Window::isFocused).findFirst().orElse(null);
    }

    /**
     * 获取窗口
     *
     * @param node
     * @return
     */
    public static Window getWindow(Node node) {
        return node.getParent().getScene().getWindow();
    }

    /**
     * 矩形裁剪
     *
     * @param region ：裁剪区域
     * @return Rectangle
     */
    public static Rectangle clipRect(Region region) {
        return clipRect(region, new SimpleDoubleProperty(0));
    }

    /**
     * 矩形裁剪
     *
     * @param node    ：裁剪区域
     * @param bindArc ：裁剪圆角
     * @return Rectangle
     */
    public static Rectangle clipRect(Node node, DoubleProperty bindArc) {
        Rectangle rectangle = new Rectangle();
        //rectangle.setSmooth(false);
        rectangle.widthProperty().bind(Bindings
                .createObjectBinding((Callable<Number>) () ->
                        node.getLayoutBounds().getWidth(), node.layoutBoundsProperty()));
        rectangle.heightProperty().bind(Bindings
                .createObjectBinding((Callable<Number>) () ->
                        node.getLayoutBounds().getHeight(), node.layoutBoundsProperty()));
        rectangle.arcWidthProperty().bind(bindArc);
        rectangle.arcHeightProperty().bind(bindArc);
        node.setClip(rectangle);
        return rectangle;
    }

    public static final String MAIN_MODAL_ID = "modal-pane";

    /**
     * 设置子节点距AnchorPane锚点布局四边的距离
     *
     * @param node
     * @param insets
     */
    public static void setAnchors(Node node, Insets insets) {
        if (insets.getTop() >= 0) {
            AnchorPane.setTopAnchor(node, insets.getTop());
        }
        if (insets.getRight() >= 0) {
            AnchorPane.setRightAnchor(node, insets.getRight());
        }
        if (insets.getBottom() >= 0) {
            AnchorPane.setBottomAnchor(node, insets.getBottom());
        }
        if (insets.getLeft() >= 0) {
            AnchorPane.setLeftAnchor(node, insets.getLeft());
        }
    }

    /**
     * 添加通知
     *
     * @param message
     */
    public static void addNoticeList(String message) {
        if (message.length() > 200) {
            message = message.substring(0, 200);
        }
        Text text = new Text(message);
        text.setWrappingWidth(500d);
        Button removeButton = new Button(null, new FontIcon(BootstrapIcons.X));
        removeButton.getStyleClass().addAll(Styles.BUTTON_ICON, Styles.FLAT, Styles.ACCENT);
        HBox hBox = new HBox(10, text, removeButton);
        hBox.setAlignment(Pos.CENTER);
        HBox.setHgrow(text, Priority.ALWAYS);
        removeButton.setOnMouseClicked(event -> FxApplicationContextUtils.HISTORY_NOTICE_LIST.remove(hBox));
        Platform.runLater(() -> FxApplicationContextUtils.HISTORY_NOTICE_LIST.add(0, hBox));
    }
}
